using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CodePlex.Resourceful.Amazon.S3;
using CodePlex.Resourceful;

namespace CodePlex.SpaceBlock.UI.Controls.Explore
{
    public partial class NewBucketControl : BaseControl
    {
        public NewBucketControl()
        {
            InitializeComponent();

            _infoGrid.RowHeadersVisible = false;
            _infoGrid.ColumnHeadersVisible = false;
            _infoGrid.ReadOnly = true;
            _infoGrid.AllowUserToOrderColumns = false;
            _infoGrid.AllowUserToAddRows = false;
            _infoGrid.AllowUserToResizeRows = false;
            _infoGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            _infoGrid.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(0xf8, 0xf8, 0xf8);

            DataGridViewImageColumn c = new DataGridViewImageColumn();
            c.Name = "Image";
            _infoGrid.Columns.Add(c);
            _infoGrid.Columns[0].DataPropertyName = "Image";
            _infoGrid.Columns[0].Width = 18;
            _infoGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
            _infoGrid.Columns.Add("Message", "");
            _infoGrid.Columns[1].DataPropertyName = "Message";
            _infoGrid.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

     
        private class ValidationMessage : NotifyObject
        {
         
            private Image _image;
            private string _message;

            public Image Image
            {
                get { return _image; }
                set { this.Set(value, "Image", ref _image); }
            }

            public string Message
            {
                get { return _message; }
                set { this.Set(value, "Message", ref _message); }
            }


        }

        public Datacenter SelectedDatacenter
        {
            get
            {
                return (Datacenter)_datacenterBox.SelectedItem;
            }
        }


        public void Initialize(S3Connection connection)
        {
            BindingList<ValidationMessage> messages = new BindingList<ValidationMessage>();
            messages.Add(new ValidationMessage());
            messages.Add(new ValidationMessage());
            messages.Add(new ValidationMessage());
            messages.Add(new ValidationMessage());
            messages.Add(new ValidationMessage());

            messages[0].Message = Properties.Messages.NewBucket_IsAllowed;
            messages[1].Message = Properties.Messages.NewBucket_IsValid;
            messages[2].Message = Properties.Messages.NewBucket_IsValidDNS;
            messages[3].Message = Properties.Messages.NewBucket_IsValidDNSMicrosoft;
            messages[4].Message = Properties.Messages.NewBucket_IsAvailable;


            this.ParentChanged += delegate
            {
                InputboxForm parent = this.FindForm() as InputboxForm;

                EventHandler refresh = delegate
               {
                   string bucket = parent.ValueBox.Text.Trim();

                  

                   bool isAllowed = connection.IsBucketValid(bucket, BucketNaming.Allowed);
                   bool isValid = connection.IsBucketValid(bucket, BucketNaming.Valid);
                   bool isValidDNS = connection.IsBucketValid(bucket, BucketNaming.ValidDNS);
                   bool isValidHost = Utils.CheckHostName(bucket + "." + S3Constants.Host);
                   bool isAvailable = isAllowed && connection.IsBucketAvailable(bucket);

                   messages[0].Image = UIManager.Instance.GetSmallImage(isAllowed ? UIConstants.ImageHints.Valid : UIConstants.ImageHints.Invalid);
                   messages[1].Image = UIManager.Instance.GetSmallImage(isValid ? UIConstants.ImageHints.Valid : UIConstants.ImageHints.Invalid);
                   messages[2].Image = UIManager.Instance.GetSmallImage(isValidDNS ? UIConstants.ImageHints.Valid : UIConstants.ImageHints.Invalid);
                   messages[3].Image = UIManager.Instance.GetSmallImage(isValidHost ? UIConstants.ImageHints.Valid : UIConstants.ImageHints.Invalid);
                   messages[4].Image = UIManager.Instance.GetSmallImage(isAvailable ? UIConstants.ImageHints.Valid : UIConstants.ImageHints.Invalid);

                   parent.OKButton.Enabled = isAvailable
                        && (this.SelectedDatacenter == Datacenter.US && isAllowed || (isValidDNS && isValidHost));
                
               };

                parent.ValueBox.TextChanged += refresh;
                _datacenterBox.SelectionChangeCommitted += refresh;
                refresh(null, null);

            };

            _infoGrid.DataSource = messages;

            _datacenterBox.DropDownStyle = ComboBoxStyle.DropDownList;
            _datacenterBox.DataSource = Enum.GetValues(typeof(Datacenter));

        }

    }
}
